package cn.iocoder.yudao.module.forward.controller.admin.eqr;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.forward.controller.admin.eqr.vo.*;
import cn.iocoder.yudao.module.forward.dal.dataobject.eqr.EqrDO;
import cn.iocoder.yudao.module.forward.service.eqr.EqrService;

@Tag(name = "管理后台 - 速报信息")
@RestController
@RequestMapping("/forward/eqr")
@Validated
public class EqrController {

    @Resource
    private EqrService eqrService;

    @PostMapping("/create")
    @Operation(summary = "创建速报信息")
    @PreAuthorize("@ss.hasPermission('forward:eqr:create')")
    public CommonResult<Integer> createEqr(@Valid @RequestBody EqrSaveReqVO createReqVO) {
        return success(eqrService.createEqr(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新速报信息")
    @PreAuthorize("@ss.hasPermission('forward:eqr:update')")
    public CommonResult<Boolean> updateEqr(@Valid @RequestBody EqrSaveReqVO updateReqVO) {
        eqrService.updateEqr(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除速报信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('forward:eqr:delete')")
    public CommonResult<Boolean> deleteEqr(@RequestParam("id") Integer id) {
        eqrService.deleteEqr(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得速报信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('forward:eqr:query')")
    public CommonResult<EqrRespVO> getEqr(@RequestParam("id") Integer id) {
        EqrDO eqr = eqrService.getEqr(id);
        return success(BeanUtils.toBean(eqr, EqrRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得速报信息分页")
    @PreAuthorize("@ss.hasPermission('forward:eqr:query')")
    public CommonResult<PageResult<EqrRespVO>> getEqrPage(@Valid EqrPageReqVO pageReqVO) {
        PageResult<EqrDO> pageResult = eqrService.getEqrPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, EqrRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出速报信息 Excel")
    @PreAuthorize("@ss.hasPermission('forward:eqr:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportEqrExcel(@Valid EqrPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<EqrDO> list = eqrService.getEqrPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "速报信息.xls", "数据", EqrRespVO.class,
                        BeanUtils.toBean(list, EqrRespVO.class));
    }

}